﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tp.Dto.Allgemein;
using Tp.Dto.Stammdaten;

namespace Tp.Services.DataLayer {
    internal class OrganisationDataLayer : BaseSqlDataLayer {
        public virtual OrganisationDto LoadOrganisation(int organisatinID) {
            string sql = "dbo.GetOrganisation @organisationID";
            using (IDataReader reader = this.GetDataReader(sql, new SqlParameter("@organisationID", organisatinID))) {
                return (reader.Read() ? LoadOrganisationFromDataReader(reader) : null);
            }
        }

        protected virtual OrganisationDto LoadOrganisationFromDataReader(IDataReader reader) {
            bool istLieferant = false;

            RechtsformDto rechtsForm = new RechtsformDto(
                  this.GetInt32(reader, "RechtsformID"),
                  this.GetString(reader, "Rechtsform")
                  );
            if (rechtsForm.RechtsformDtoId == 0) {
                rechtsForm.RechtsformDtoId = -1;
                rechtsForm.Name = "<Bitte auswählen>";
            }

            AddressTypeDto adrType = new AddressTypeDto(
                  this.GetInt32(reader, "AdressenTypeID"),
                  this.GetString(reader, "AdressenType")
                  );

            AddressDto adr = new AddressDto(
                  this.GetInt32(reader, "AdressenID"),
                  adrType,
                  this.GetString(reader, "Plz"),
                  this.GetString(reader, "Ort"),
                  this.GetString(reader, "Strasse"),
                  this.GetString(reader, "Hausnummer"),
                  this.GetString(reader, "Vermerk"),
                  this.GetByte(reader, "Zone"),
                  this.GetDecimal(reader, "Entfernung"),
                  this.GetString(reader, "Land")
                  );

            TelefonDto tel = new TelefonDto(
                  this.GetInt32(reader, "TelKontaktID"),
                  this.GetString(reader, "TelKontaktPrefix"),
                  this.GetString(reader, "TelKontaktWert"),
                  this.GetString(reader, "TElKontaktSuffix")
                  );

            //TelefonDto mob = new TelefonDto(
            //	  this.GetInt32(reader, "MobKontaktID"),
            //	  this.GetString(reader, "MobKontaktPrefix"),
            //	  this.GetString(reader, "MobKontaktWert"),
            //	  this.GetString(reader, "MobKontaktSuffix")
            //	  );

            TelefonDto fax = new TelefonDto(
                  this.GetInt32(reader, "FaxKontaktID"),
                  this.GetString(reader, "FaxKontaktPrefix"),
                  this.GetString(reader, "FaxKontaktWert"),
                  this.GetString(reader, "FaxKontaktSuffix")
                  );

            OrganisationTypeDto orgType = new OrganisationTypeDto(
                  this.GetInt32(reader, "OrganisationTypeID"),
                  this.GetString(reader, "OrganisationType")
                  );

            if (orgType.OrganisationTypeDtoId == 0) {
                orgType.OrganisationTypeDtoId = -1;
                orgType.Name = "<Bitte auswählen>";
            }

            if (this.GetInt32(reader, "OrganisationID") == 2) {
                istLieferant = true;
            }
            OrganisationDto org = new OrganisationDto(
                  this.GetInt32(reader, "OrganisationID"),
                  this.GetString(reader, "Prefix"),
                  this.GetString(reader, "Name"),
                  this.GetString(reader, "name2"),
                  rechtsForm,
                  this.GetString(reader, "Suchbegriffe"),
                  orgType,
                  adr,
                  tel,
                  fax,
                  this.GetString(reader, "eMail"),
                  this.GetString(reader, "Homepage"),
                  istLieferant,
                  false,
                  this.GetString(reader, "Erstkontakt"),
                  this.GetDateTime(reader, "ErstkontaktDatum"),
                  this.GetString(reader, "ErstkontaktMa")
                  );

            
            org.Abteilung = this.GetString(reader, "Abteilung");

            //reader.NextResult();



            return org;
        }
    }
}
